package es.cazadoresmadridejos.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import es.cazadoresmadridejos.web.decorator.DateDecorator;

@Entity(name="BigGame")
@Table(name="monteria")
public class BigGame extends BaseEntity	{

	private static final long serialVersionUID = -5332842901911766525L;
	
	public enum BigGameName{
		UMBRION, MINGOLIVA, CALVACHES,HUERTA, VASTO, ZALVALEA,CABALGADOR
	}
	
	public enum BigGameStatus{
		ABIERTA, EN_PROCESO, CERRADA
	}
	
	private Reason reason;
	private Date date;
	private BigDecimal amount;
	private Integer deers;
	private Integer hinds;
	private Integer wildBoars;
	private String description;
	private Integer stands;
	private BigGameName name;
	private String image;
	private List<BigGameAssistance> assistances = new ArrayList<BigGameAssistance>();
	private String arrivalHour;
	private String raffleHour;
	private BigGameStatus status;
	
	@ManyToOne(optional=false)
	@JoinColumn(name="temporada")	
	public Reason getReason() {
		return reason;
	}
	public void setReason(Reason reason) {
		this.reason = reason;
	}
	@Column(name="fecha")
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	@Column(name="precio")
	public BigDecimal getAmount() {
		return amount;
	}
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	@Column(name="venados")
	public Integer getDeers() {
		return deers;
	}
	public void setDeers(Integer deers) {
		this.deers = deers;
	}
	@Column(name="ciervas")
	public Integer getHinds() {
		return hinds;
	}
	public void setHinds(Integer hinds) {
		this.hinds = hinds;
	}
	@Column(name="jabalies")
	public Integer getWildBoars() {
		return wildBoars;
	}
	public void setWildBoars(Integer wildBoars) {
		this.wildBoars = wildBoars;
	}
	@Column(name="comentarios")
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Column(name="puestos")
	public Integer getStands() {
		return stands;
	}
	public void setStands(Integer stands) {
		this.stands = stands;
	}
	@Column(name="nombre")
	@Enumerated(EnumType.STRING)
	public BigGameName getName() {
		return name;
	}
	public void setName(BigGameName name) {
		this.name = name;
	}
	@Column(name="imagen")
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	
	@OneToMany(mappedBy="bigGame",cascade=javax.persistence.CascadeType.ALL,fetch=FetchType.EAGER)
	@Cascade(CascadeType.DELETE_ORPHAN)
	public List<BigGameAssistance> getAssistances() {
		return assistances;
	}
	public void setAssistances(List<BigGameAssistance> assistances) {
		this.assistances = assistances;
	}

	
	@Transient
	public String getDecoratedDate(){
		return DateDecorator.getDecoratedDate(getDate());
	}
	
	@Column(name="hora_sorteo")
	public String getRaffleHour() {
		return this.raffleHour;
	}
	
	@Column(name="hora_llegada")
	public String getArrivalHour() {
		return this.arrivalHour;
	}
	public void setArrivalHour(String arrivalHour) {
		this.arrivalHour = arrivalHour;
	}
	public void setRaffleHour(String raffleHour) {
		this.raffleHour = raffleHour;
	}
	
	@Transient
	public String getArrivalHour1(){
		if(this.arrivalHour == null){
			return "8";
		}
		if(this.arrivalHour.indexOf(":") == -1){
			return this.arrivalHour;
		}
		return this.arrivalHour.substring(0,this.arrivalHour.indexOf(":"));
	}
	
	public void setArrivalHour1(String arrivalHour){
		if(arrivalHour == null){
			arrivalHour = "8";
		}
		if(this.arrivalHour == null){
			this.arrivalHour = arrivalHour;
			return;
		}
		if(this.arrivalHour.indexOf(":") != -1){
			this.arrivalHour = arrivalHour + this.arrivalHour.substring(this.arrivalHour.indexOf(":"));
			return;
		}
		this.arrivalHour = arrivalHour + ":30";
	}
	
	public void setArrivalHour2(String arrivalHour){
		if(arrivalHour == null){
			arrivalHour = "30";
		}
		if(this.arrivalHour == null){
			this.arrivalHour = "8:" + arrivalHour;
			return;
		}
		if(this.arrivalHour.indexOf(":") != -1){
			this.arrivalHour = this.arrivalHour.substring(0,this.arrivalHour.indexOf(":") + 1) + arrivalHour;
			return;
		}
		this.arrivalHour += ":" + arrivalHour;
	}
	
	@Transient
	public String getArrivalHour2(){
		if(this.arrivalHour == null){
			return "30";
		}
		if(this.arrivalHour.indexOf(":") == -1){
			return this.arrivalHour;
		}
		return this.arrivalHour.substring(this.arrivalHour.indexOf(":") + 1);
	}
	
	
	
	//Hora del sorteo
	@Transient
	public String getRaffleHour1(){
		if(this.raffleHour == null){
			return "8";
		}
		return this.raffleHour.substring(0,this.raffleHour.indexOf(":"));
	}
	
	public void setRaffleHour1(String raffleHour){
		if(raffleHour == null){
			raffleHour = "8";
		}
		if(this.raffleHour == null){
			this.raffleHour = raffleHour;
			return;
		}
		if(this.raffleHour.indexOf(":") != -1){
			this.raffleHour = raffleHour + this.raffleHour.substring(this.raffleHour.indexOf(":"));
			return;
		}
		this.raffleHour = raffleHour + ":30";
	}
	@Transient
	public String getRaffleHour2(){
		if(this.raffleHour == null){
			return "30";
		}
		return this.raffleHour.substring(this.raffleHour.indexOf(":") + 1);
	}
	
	public void setRaffleHour2(String raffleHour){
		if(raffleHour == null){
			raffleHour = "30";
		}
		if(this.raffleHour == null){
			this.raffleHour = "8:" + raffleHour;
			return;
		}
		if(this.raffleHour.indexOf(":") != -1){
			this.raffleHour = this.raffleHour.substring(0,this.raffleHour.indexOf(":") + 1) + raffleHour;
			return;
		}
		this.raffleHour += ":" + raffleHour;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(name="estado")
	public BigGameStatus getStatus() {
		return status;
	}
	public void setStatus(BigGameStatus status) {
		this.status = status;
	}
}
